﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKExcelFormTool.DataDefine;

namespace YJKExcelFormTool.Creator
{
	internal class MaterialTableCreater : TableCreator
	{
		public MaterialTableCreater(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public TableBody CreateTableBody(List<TableHeadCellKey> headerParameters, ImportMaterialTableInfos importTableInfos)
		{
			if (headerParameters.Count == 0)
			{
				return null;
			}
			List<TableBodyRow> list = new List<TableBodyRow>();
			foreach (ImportTableRow importTableRow in importTableInfos.TableData)
			{
				List<TableBodyCell> list2 = new List<TableBodyCell>();
				string category = importTableRow.Category;
				int i = 0;
				int count = headerParameters.Count;
				while (i < count)
				{
					TableHeadCellKey tableHeadCellKey = headerParameters[i];
					string keyName = tableHeadCellKey.KeyName;
					TableBodyCell tableBodyCell;
					if (tableHeadCellKey.KeyType() == TableHeadCellKey._Tag_KeyType.HT_SINGLE)
					{
						int num = importTableRow.FindCellByCellName(keyName);
						if (num == -1)
						{
							tableBodyCell = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, "");
						}
						else
						{
							ImportTableCell importTableCell = importTableRow.Row[num];
							if (importTableCell.CellType == _Tag_ImportCellValueType.CVT_STRING)
							{
								tableBodyCell = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, importTableCell.CellValue);
							}
							else if (importTableCell.CellType == _Tag_ImportCellValueType.CVT_LEGEND_NAME)
							{
								tableBodyCell = this.CreateSymbolCellData(importTableCell.CellValue);
								if (tableBodyCell == null)
								{
									tableBodyCell = new TableBodyCell(TableBodyCell._tagDataType._DT_NULL, "");
								}
							}
							else
							{
								if (importTableCell.CellType != _Tag_ImportCellValueType.CVT_SYSFAMILY_LEGEND_NAME)
								{
									throw new Exception("null type");
								}
								tableBodyCell = this.CreateSysMamilySymbolCellData(importTableCell.CellValue);
								if (tableBodyCell == null)
								{
									tableBodyCell = new TableBodyCell(TableBodyCell._tagDataType._DT_NULL, "");
								}
							}
						}
					}
					else if (tableHeadCellKey.KeyType() == TableHeadCellKey._Tag_KeyType.HT_COMPOSITE)
					{
						string text = "";
						List<string> list3 = new List<string>();
						List<string> list4 = new List<string>();
						tableHeadCellKey.FindAttributes2ndDelimitersByCategory(category, ref list3, ref list4);
						for (int j = 0; j < list3.Count; j++)
						{
							string cellName = list3[j];
							int num2 = importTableRow.FindCellByCellName(cellName);
							if (num2 != -1)
							{
								ImportTableCell importTableCell2 = importTableRow.Row[num2];
								if (j == list3.Count - 1)
								{
									text += importTableCell2.CellValue;
								}
								else
								{
									text = text + importTableCell2.CellValue + list4[j];
								}
							}
						}
						tableBodyCell = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, text);
					}
					else
					{
						tableBodyCell = new TableBodyCell(TableBodyCell._tagDataType._DT_TEXT, "");
					}
					list2.Add(tableBodyCell);
					i++;
				}
				TableBodyRow item = new TableBodyRow(list2);
				list.Add(item);
			}
			return new TableBody(list);
		}

		private TableBodyCell CreateSymbolCellData(string legendFileFillName)
		{
			return new TableBodyCell(TableBodyCell._tagDataType._DT_SYMBOL, legendFileFillName);
		}

		private TableBodyCell CreateSysMamilySymbolCellData(string legendFileFillName)
		{
			return new TableBodyCell(TableBodyCell._tagDataType._DT_SYMBOL, legendFileFillName);
		}

		public bool GetStatisticsDetailsWithMaterialTable(string tableSettingFile, string programId, ref List<string> selectedCategoryNameList, ref List<CombinationOfAttributes> combinationOfAttributesList, ref List<MaterialProgramInfos.FilterType> filterTypeList, ref string message)
		{
			MaterialProgramInfos materialProgramInfos = null;
			this.ReadTableSetting(tableSettingFile, programId, ref materialProgramInfos);
			foreach (MaterialProgramInfos.StatisticalCategory statisticalCategory in materialProgramInfos.StatisticalCategoryList)
			{
				if (statisticalCategory.m_Checked)
				{
					selectedCategoryNameList.Add(statisticalCategory.m_Name);
				}
			}
			TableInfos tableInfos = materialProgramInfos.TableInfos;
			if (tableInfos.IsNull())
			{
				return false;
			}
			foreach (TableHeadCell tableHeadCell in tableInfos.Head.GetEffectiveRow())
			{
				if (tableHeadCell.Key.KeyType() == TableHeadCellKey._Tag_KeyType.HT_COMPOSITE)
				{
					foreach (CombinationOfAttributes combinationOfAttributes in tableHeadCell.Key.CombinationOfAttributes)
					{
						string categortyName = combinationOfAttributes.CategoryName;
						int num = selectedCategoryNameList.FindIndex((string s) => s.CompareTo(categortyName) == 0);
						if (num != -1)
						{
							num = combinationOfAttributesList.FindIndex((CombinationOfAttributes s) => s.CategoryName.CompareTo(categortyName) == 0);
							if (num == -1)
							{
								combinationOfAttributesList.Add(combinationOfAttributes);
							}
							else
							{
								combinationOfAttributesList[num].AttributesName.AddRange(combinationOfAttributes.AttributesName);
							}
						}
					}
				}
			}
			filterTypeList = materialProgramInfos.FilterTypeList;
			return true;
		}

		public bool ReadTableSetting(string tableSettingFile, string programId, ref MaterialProgramInfos materialProgramInfos)
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(tableSettingFile);
			}
			catch (Exception)
			{
				return false;
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("ProgramList");
			if (xmlNode == null)
			{
				return false;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			XmlNode xmlNode2 = null;
			foreach (object obj in childNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if ((xmlNode3.SelectSingleNode("BasicInfos") as XmlElement).GetAttribute("Id").CompareTo(programId) == 0)
				{
					xmlNode2 = xmlNode3;
					break;
				}
			}
			if (xmlNode2 != null)
			{
				XmlElement xmlElement = xmlNode2.SelectSingleNode("BasicInfos") as XmlElement;
				string attribute = xmlElement.GetAttribute("Name");
				string attribute2 = xmlElement.GetAttribute("Describe");
				string attribute3 = xmlElement.GetAttribute("Id");
				XmlNodeList childNodes2 = (xmlNode2.SelectSingleNode("StatisticalCategory") as XmlElement).ChildNodes;
				List<MaterialProgramInfos.StatisticalCategory> list = new List<MaterialProgramInfos.StatisticalCategory>();
				foreach (object obj2 in childNodes2)
				{
					XmlElement xmlElement2 = ((XmlNode)obj2) as XmlElement;
					string attribute4 = xmlElement2.GetAttribute("Name");
					bool @checked = Convert.ToBoolean(xmlElement2.GetAttribute("Checked"));
					list.Add(new MaterialProgramInfos.StatisticalCategory
					{
						m_Name = attribute4,
						m_Checked = @checked
					});
				}
				XmlElement xmlElement3 = (xmlNode2.SelectSingleNode("TableHead") as XmlElement).SelectSingleNode("Setting") as XmlElement;
				TableHead._Tag_HeadType tableHeadType = (TableHead._Tag_HeadType)Convert.ToInt32(xmlElement3.GetAttribute("HeadType"));
				bool autoCellsSize = Convert.ToBoolean(xmlElement3.GetAttribute("AutoCellsSize"));
				string attribute5 = xmlElement3.GetAttribute("FontSettingName");
				TextNoteType textNoteType = base.GetTextNoteType(attribute5);
				double hMargin = Convert.ToDouble(xmlElement3.GetAttribute("HMargin"));
				double vMargin = Convert.ToDouble(xmlElement3.GetAttribute("VMargin"));
				XmlElement xmlElement4 = xmlElement3.SelectSingleNode("SourceInfo") as XmlElement;
				string attribute6 = xmlElement4.GetAttribute("Name");
				int sheetIndex = Convert.ToInt32(xmlElement4.GetAttribute("SheetIndex"));
				int badyStartRowNumber = Convert.ToInt32(xmlElement4.GetAttribute("BadyStartRowNumber"));
				int badyEndRowNumber = Convert.ToInt32(xmlElement4.GetAttribute("BadyEndRowNumber"));
				TableSourceInfo tableSourceInfo = new TableSourceInfo(attribute6, sheetIndex, badyStartRowNumber, badyEndRowNumber);
				List<TableHeadCell> tableHeadInfos = new List<TableHeadCell>();
				TableCreator.ReadHeadCellList(xmlElement3.SelectSingleNode("HeadInfos") as XmlElement, ref tableHeadInfos);
				XmlElement xmlElement5 = xmlElement3.SelectSingleNode("HeadColumnsWidth") as XmlElement;
				List<double> list2 = new List<double>();
				int num = Convert.ToInt32(xmlElement5.GetAttribute("Count"));
				for (int i = 0; i < num; i++)
				{
					XmlElement xmlElement6 = xmlElement5.SelectSingleNode("ColWidth" + (i + 1).ToString()) as XmlElement;
					list2.Add(Convert.ToDouble(xmlElement6.InnerText));
				}
				XmlElement xmlElement7 = xmlElement3.SelectSingleNode("HeadRowsHeight") as XmlElement;
				List<double> list3 = new List<double>();
				int num2 = Convert.ToInt32(xmlElement7.GetAttribute("Count"));
				for (int i = 0; i < num2; i++)
				{
					XmlElement xmlElement8 = xmlElement7.SelectSingleNode("RowHeight" + (i + 1).ToString()) as XmlElement;
					list3.Add(Convert.ToDouble(xmlElement8.InnerText));
				}
				TableInfos tableInfos = new TableInfos(new TableHead(tableHeadType, tableSourceInfo, tableHeadInfos, num2, num, list3, list2), null, textNoteType, hMargin, vMargin, autoCellsSize);
				XmlNodeList childNodes3 = (xmlNode2.SelectSingleNode("FilterTypeList") as XmlElement).ChildNodes;
				List<MaterialProgramInfos.FilterType> list4 = new List<MaterialProgramInfos.FilterType>();
				foreach (object obj3 in childNodes3)
				{
					XmlElement xmlElement9 = ((XmlNode)obj3) as XmlElement;
					list4.Add(new MaterialProgramInfos.FilterType
					{
						m_Type = xmlElement9.GetAttribute("Type"),
						m_Condition = xmlElement9.GetAttribute("Condition"),
						m_Value = xmlElement9.GetAttribute("Value")
					});
				}
				materialProgramInfos = new MaterialProgramInfos(attribute, attribute2, attribute3, tableInfos, list, list4);
				return true;
			}
			return false;
		}

		public void WriteTableSetting(MaterialProgramInfos materialProgramInfos, string tableSettingFile, string programId)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlElement xmlElement = null;
			XmlNode xmlNode = null;
			if (File.Exists(tableSettingFile))
			{
				try
				{
					xmlDocument.Load(tableSettingFile);
				}
				catch (Exception)
				{
					return;
				}
				xmlElement = (xmlDocument.SelectSingleNode("ProgramList") as XmlElement);
				if (xmlElement == null)
				{
					return;
				}
				IEnumerator enumerator = xmlElement.ChildNodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						XmlNode xmlNode2 = (XmlNode)obj;
						if ((xmlNode2.SelectSingleNode("BasicInfos") as XmlElement).GetAttribute("Id").CompareTo(programId) == 0)
						{
							xmlNode = xmlNode2;
							break;
						}
					}
					goto IL_103;
				}
			}
			XmlNode xmlNode3 = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
			XmlNode xmlNode4 = xmlNode3;
			xmlNode4.InnerText += " encoding=\"GB2312\"";
			xmlDocument.AppendChild(xmlNode3);
			xmlElement = xmlDocument.CreateElement("", "ProgramList", "");
			xmlElement.SetAttribute("version", "3.0");
			xmlDocument.AppendChild(xmlElement);
			IL_103:
			TableInfos tableInfos = materialProgramInfos.TableInfos;
			XmlElement xmlElement2 = xmlDocument.CreateElement("", "Program", "");
			if (xmlNode == null)
			{
				xmlElement.AppendChild(xmlElement2);
			}
			else
			{
				xmlElement.ReplaceChild(xmlElement2, xmlNode);
			}
			XmlElement xmlElement3 = xmlDocument.CreateElement("BasicInfos");
			xmlElement3.SetAttribute("Name", materialProgramInfos.Name);
			xmlElement3.SetAttribute("Describe", materialProgramInfos.Describe);
			xmlElement3.SetAttribute("Id", materialProgramInfos.Id);
			xmlElement3.SetAttribute("TemplateType", ((int)materialProgramInfos.TableInfos.Head.HeadType).ToString());
			xmlElement2.AppendChild(xmlElement3);
			XmlElement xmlElement4 = xmlDocument.CreateElement("StatisticalCategory");
			foreach (MaterialProgramInfos.StatisticalCategory statisticalCategory in materialProgramInfos.StatisticalCategoryList)
			{
				XmlElement xmlElement5 = xmlDocument.CreateElement("Category");
				xmlElement5.SetAttribute("Name", statisticalCategory.m_Name);
				XmlElement xmlElement6 = xmlElement5;
				string name = "Checked";
				bool @checked = statisticalCategory.m_Checked;
				xmlElement6.SetAttribute(name, @checked.ToString());
				xmlElement4.AppendChild(xmlElement5);
			}
			xmlElement2.AppendChild(xmlElement4);
			XmlElement xmlElement7 = xmlDocument.CreateElement("TableHead");
			XmlElement xmlElement8 = xmlDocument.CreateElement("Setting");
			xmlElement8.SetAttribute("HeadType", ((int)tableInfos.TableHeadType).ToString());
			xmlElement8.SetAttribute("AutoCellsSize", tableInfos.AutoCellsSize.ToString());
			xmlElement8.SetAttribute("FontSettingName", tableInfos.FontName());
			xmlElement8.SetAttribute("HMargin", tableInfos.HMargin.ToString());
			xmlElement8.SetAttribute("VMargin", tableInfos.VMargin.ToString());
			XmlElement xmlElement9 = xmlDocument.CreateElement("SourceInfo");
			xmlElement9.SetAttribute("Name", tableInfos.Head.TableSourceInfo.Name);
			xmlElement9.SetAttribute("SheetIndex", tableInfos.Head.TableSourceInfo.SheetIndex.ToString());
			xmlElement9.SetAttribute("BadyStartRowNumber", tableInfos.Head.TableSourceInfo.BadyStartRowNumber.ToString());
			xmlElement9.SetAttribute("BadyEndRowNumber", tableInfos.Head.TableSourceInfo.BadyEndRowNumber.ToString());
			xmlElement8.AppendChild(xmlElement9);
			XmlElement xmlElement10 = xmlDocument.CreateElement("HeadInfos");
			List<TableHeadCell> tableHeadInfos = tableInfos.Head.TableHeadInfos;
			xmlElement10.SetAttribute("HeadCellCount", tableHeadInfos.Count.ToString());
			TableCreator.WriteHeadCellList(xmlDocument, tableHeadInfos, ref xmlElement10);
			xmlElement8.AppendChild(xmlElement10);
			XmlElement xmlElement11 = xmlDocument.CreateElement("HeadColumnsWidth");
			List<double> cellWidthList = tableInfos.Head.GetCellWidthList();
			xmlElement11.SetAttribute("Count", cellWidthList.Count.ToString());
			for (int i = 0; i < cellWidthList.Count; i++)
			{
				double num = cellWidthList[i];
				XmlElement xmlElement12 = xmlDocument.CreateElement("ColWidth" + (i + 1).ToString());
				xmlElement12.InnerText = num.ToString();
				xmlElement11.AppendChild(xmlElement12);
			}
			xmlElement8.AppendChild(xmlElement11);
			XmlElement xmlElement13 = xmlDocument.CreateElement("HeadRowsHeight");
			List<double> cellHeightList = tableInfos.Head.GetCellHeightList();
			xmlElement13.SetAttribute("Count", cellHeightList.Count.ToString());
			for (int i = 0; i < cellHeightList.Count; i++)
			{
				double num2 = cellHeightList[i];
				XmlElement xmlElement14 = xmlDocument.CreateElement("RowHeight" + (i + 1).ToString());
				xmlElement14.InnerText = num2.ToString();
				xmlElement13.AppendChild(xmlElement14);
			}
			xmlElement8.AppendChild(xmlElement13);
			xmlElement7.AppendChild(xmlElement8);
			xmlElement2.AppendChild(xmlElement7);
			XmlElement xmlElement15 = xmlDocument.CreateElement("FilterTypeList");
			foreach (MaterialProgramInfos.FilterType filterType in materialProgramInfos.FilterTypeList)
			{
				XmlElement xmlElement16 = xmlDocument.CreateElement("FilterType");
				xmlElement16.SetAttribute("Type", filterType.m_Type);
				xmlElement16.SetAttribute("Condition", filterType.m_Condition);
				xmlElement16.SetAttribute("Value", filterType.m_Value);
				xmlElement15.AppendChild(xmlElement16);
			}
			xmlElement2.AppendChild(xmlElement15);
			try
			{
				xmlDocument.Save(tableSettingFile);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}
	}
}
